﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;

namespace 浏览器客户端.Helper
{
    public static class Common
    {
        /// <summary>
        /// 加载配置文件
        /// </summary>
        /// <param name="filepath"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T LoadConfig<T>(string filepath = "config.json")
        {
            if (!File.Exists(filepath)) return default;
            var json = File.ReadAllText(filepath);
            if (!json.TryJsonParse(out T data, out _))
            {
                return default;
            }
            return data;
        }

        /// <summary>
        /// 判断字符串是否为空
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool IsNullOrWhiteSpace(this string content)
        {
            return string.IsNullOrWhiteSpace(content);
        }

        /// <summary>
        /// 保存配置文件
        /// </summary>
        /// <param name="config"></param>
        /// <param name="filepath"></param>
        /// <typeparam name="T"></typeparam>
        public static void SaveConfig<T>(T config, string filepath = "config.json")
        {
            var json = JsonConvert.SerializeObject(config);
            File.WriteAllText(filepath, json);
        }

        /// <summary>
        /// 解析字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static bool TryJsonParse<T>(this string json, out T data)
        {
            data = default;
            try
            {
                data = JsonConvert.DeserializeObject<T>(json);
                if (data == null) return false;
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }



        /// <summary>
        /// 解析字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <param name="data"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static bool TryJsonParse<T>(this string json, out T data, out string error)
        {
            data = default;
            error = "";
            try
            {
                data = JsonConvert.DeserializeObject<T>(json);
                if (data == null) return false;
                return true;
            }
            catch (Exception ex)
            {
                error = JsonConvert.SerializeObject(ex);
                return false;
            }
        }

        /// <summary>
        /// 解析字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <param name="data"></param>
        /// <param name="error"></param>
        /// <param name="setting"></param>
        /// <returns></returns>
        public static bool TryJsonParse<T>(this string json, out T data, out string error, JsonSerializerSettings setting)
        {
            if (setting == null)
            {
                return json.TryJsonParse(out data, out error);
            }

            data = default;
            error = "";
            try
            {
                data = JsonConvert.DeserializeObject<T>(json, setting);
                if (data == null) return false;
                return true;
            }
            catch (Exception ex)
            {
                error = JsonConvert.SerializeObject(ex);
                return false;
            }
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string Md5Hash(this string content)
        {
            using (var md5 = MD5.Create())
            {
                var result = md5.ComputeHash(Encoding.ASCII.GetBytes(content));
                var strResult = BitConverter.ToString(result);
                return strResult.Replace("-", "").ToLower();
            }
        }
    }
}
